package org.jpropelleralt.change;

/**
 * Simplest implementation of {@link Change}, just
 * implements {@link #shallow()} 
 */
public class ChangeDefault implements Change {

	private final boolean shallow;

	private final static ChangeDefault SHALLOW = new ChangeDefault(true);
	private final static ChangeDefault DEEP = new ChangeDefault(false);
	
	/**
	 * Get a {@link ChangeDefault} instance.
	 * @param instancesChanged	True if instances have changed
	 * @return	{@link ChangeDefault} instance
	 */
	public final static ChangeDefault instance(boolean instancesChanged) {
		return instancesChanged ? SHALLOW : DEEP;
	}

	
	/**
	 * Get the shallow {@link ChangeDefault} instance.
	 * @return	Shallow {@link ChangeDefault} instance.
	 */
	public final static ChangeDefault shallowInstance() {
		return SHALLOW;
	}

	/**
	 * Get the deep {@link ChangeDefault} instance.
	 * @return	Deep {@link ChangeDefault} instance.
	 */
	public final static ChangeDefault deepInstance() {
		return DEEP;
	}

	private ChangeDefault(boolean shallow) {
		this.shallow = shallow;
	}
	
	@Override
	public boolean shallow() {
		return shallow;
	}

	@Override
	public Change extend(Change existing) {
	    boolean extendedShallow = existing.shallow() || shallow;
	    if (extendedShallow != shallow) {
	      return instance(extendedShallow);
	    } else {
	      return null;
	    }
	}
	
	@Override
	public String toString() {
		return ((shallow ? "shallow" : "deep") + " change");
	}
}
